﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxDSOFramer;

#region Office命名空间
using Microsoft.Office.Interop;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using System.IO.Compression;
#endregion



namespace WoSoft.PACS.Document
{
    public class WordDocument
    {
        private AxFramerControl _myWordControl;

        private Word.Document doc = null;
        public Word.Document Doc
        {
            get { return this.doc; }
        }

        /// <summary>
        ///当前文档的活动窗口
        /// </summary>
        private Word.Window ActiveWindow
        {
            get
            {
                try
                {
                    return doc.ActiveWindow;
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    if (ex.Message.Contains("RPC 服务器不可用"))
                    {
                        msg = "Word进程异常、或者已经关闭。";
                    }

                    throw new Exception(msg, ex);
                }
            }
        }

        public WordDocument(AxFramerControl wordControl)
        {
            _myWordControl = wordControl;
        }

        void doc_ContentControlOnEnter(ContentControl ContentControl)
        {
            //控件获取焦点事件
            //if(ContentControl.Tag=="test")
            //    ContentControl.Range.Text = "test888888888888888888";
        }

        public AxDSOFramer.AxFramerControl Create()
        {
            return new AxDSOFramer.AxFramerControl();
        }

        /// <summary>
        /// 设置到书签内容
        /// </summary>
        /// <param name="bookName"></param>
        private void SetTextToWord(Microsoft.Office.Interop.Word.Document wdoc, object bookName, string value)
        {
            if (wdoc.Bookmarks.Exists(bookName.ToString()))
            {
                if (wdoc.Bookmarks[bookName] != null)
                {
                    wdoc.Bookmarks[bookName].Range.Text = value;
                }
            }
        }

        //执行打印
        public void Print()
        {//打印
            //_myWordControl.ShowDialog(DSOFramer.dsoShowDialogType.dsoDialogPrint);
            _myWordControl.PrintOut();
        }

        //打印预览
        public void PrintView()
        {
            if (object.Equals(doc, null))
                doc = _myWordControl.ActiveDocument as Word.Document;
            try
            {
                if (doc.Application.PrintPreview)
                {
                    if (doc.Application.PrintPreview)
                    {
                        doc.ClosePrintPreview();
                    }

                    ActiveWindow.View.ShowCropMarks = true;

                    //patch：解决退出预览后，文档显示空白的问题。
                    ActiveWindow.View.Type = WdViewType.wdNormalView;
                    ActiveWindow.View.Type = WdViewType.wdPrintView;
                    ActiveWindow.View.Zoom.Percentage = 100;
                }
                else
                {
                    //进入预览
                    if (!doc.Application.PrintPreview)
                    {
                        doc.PrintPreview();
                    }

                    ActiveWindow.DocumentMap = false;
                    ActiveWindow.View.ShowFieldCodes = false;
                    ActiveWindow.Thumbnails = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //保存到数据库中
        public void SaveAs()
        {
            //var xml = doc.WordOpenXML;
            _myWordControl.ShowDialog(DSOFramer.dsoShowDialogType.dsoDialogSaveCopy);
        }

        //返回当前XML
        public string GetDocXML()
        {
            //return doc.WordOpenXML;
            //return doc.Content.XML;
            return doc.Content.WordOpenXML;
        }

        /// <summary>
        /// 将病人信息传到更新
        /// </summary>
        /// <param name="elementModel"></param>
        public void UpdatePatientInfos(ElementModel elementModel,string yymc)
        {
            var activeDoc = _myWordControl.ActiveDocument as Word.Document;
            if (activeDoc != null)
            {
                doc = activeDoc;
                doc.ContentControlOnEnter += new DocumentEvents2_ContentControlOnEnterEventHandler(doc_ContentControlOnEnter);
                foreach (ContentControl item in doc.ContentControls)
                {
                    switch (item.Tag)
                    {
                        case "医院名称":
                            item.Range.Text = yymc;
                            break;
                        case "姓名":
                            item.Range.Text = elementModel.Name;
                            break;
                        case "性别":
                            item.Range.Text = elementModel.Sex;
                            break;
                        case "年龄":
                            item.Range.Text = elementModel.Age;
                            break;
                        case "申请号":
                            item.Range.Text = elementModel.AppOrderNum;
                            break;
                        case "床号":
                            item.Range.Text = elementModel.BedNum == "" ? " " : elementModel.BedNum;
                            break;
                        case "申请科室":
                            item.Range.Text = elementModel.AppDepartment == "" ? " " : elementModel.AppDepartment;
                            break;
                        case "申请医生":
                            item.Range.Text = elementModel.AppDoctor == "" ? " " : elementModel.AppDoctor;
                            break;
                        case "检查号":
                            item.Range.Text = elementModel.CheckID;
                            break;
                        case "检查项目":
                            item.Range.Text = elementModel.CheckItem;
                            break;
                        case "临床诊断":
                            item.Range.Text = elementModel.Diagnosis;
                            break;
                        case "申请日期":
                            item.Range.Text = elementModel.CheckTime;
                            break;
                        case "检查科室":
                            item.Range.Text = elementModel.CheckDepartment;
                            break;
                        case "检查医师":
                            item.Range.Text = elementModel.CheckDoctor;
                            break;
                        case "审核医师":
                            item.Range.Text = elementModel.AuditDoctor == "" ? " " : elementModel.AuditDoctor;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        public void AddWordText(string text1, string text2, string text3)
        {
            var activeDoc = _myWordControl.ActiveDocument as Word.Document;
            if (activeDoc != null)
            {
                doc = activeDoc;
                foreach (ContentControl item in doc.ContentControls)
                {
                    switch (item.Tag)
                    {
                        case "检查印象":
                            item.Range.Text = text1;
                            break;
                        case "诊断建议":
                            item.Range.Text = text2;
                            break;
                        case "检查备注":
                            item.Range.Text = text2;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}
